package com.test.javafaq_portal.controller;


import com.test.javafaq_portal.entity.Comment;
import com.test.javafaq_portal.service.ICommentService;
import com.test.javafaq_portal.vo.CommentVo;
import com.test.javafaq_portal.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test.com
 * @since 2025-04-11
 */
@RestController
@RequestMapping("/v1/comment")
@Slf4j
public class CommentController {
    @Autowired
    ICommentService commentService;
    @PostMapping
    public R<Comment> postComment(@Validated CommentVo commentVo, BindingResult res, @AuthenticationPrincipal UserDetails user){
        if(res.hasErrors()){
            String msg = res.getFieldError().getDefaultMessage();
            return R.unproecsableEntity(msg);
        }
        log.debug("开始新增评论：{}",commentVo);

        return R.create(commentService.saveComment(commentVo, user.getUsername()));
    }

}
